package com.project;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListItem extends ListActivity{

	private static final SQLiteDatabase SQLiteDatabase = null;
	private DBHelper dbHelp;
	private Cursor cur;
	private ArrayList<String> listName = new ArrayList<String>();
	private ArrayList<Integer> listId = new ArrayList<Integer>();
	private String[] realList;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	
        //Toast.makeText(this, "ListItem", Toast.LENGTH_LONG).show();
        
        dbHelp = new DBHelper(this);
        cur = dbHelp.allPin();
        while(cur.moveToNext()){
        	listName.add(cur.getString(cur.getColumnIndex(DBHelper.colName)));
        	listId.add(cur.getInt(cur.getColumnIndex(DBHelper.colID)));
        }
        realList = new String[listName.size()];
        
        int count = 0;
        for(String item : listName){
        	realList[count] = item;
        	count++;
        }
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,listName));
	}

	protected void onListItemClick(ListView l, View v,int position, long id){
	    	super.onListItemClick(l, v, position, id);
	    	Object o = this.getListAdapter().getItem(position);
	    	String keyword = o.toString();
	    	Intent intent;
	    	Bundle bundle = new Bundle();
			
	    	for(int i=0;i<realList.length;i++){
		    	if(keyword.equals(realList[i])){
		    		intent = new Intent(this, MapView.class);
		    		int index = listName.indexOf(keyword);
		    		bundle.putInt("id", listId.get(index));
		    		intent.putExtras(bundle);
		    		startActivity(intent);
		    	}
	    	}
	}

}
